/**
 * @file
 * @auth jinguangguo
 * @date 2016/12/29
 */

import Vue from 'vue';
import BigNumber from 'bignumber.js';
import VueAnimateNumber from '../../../../grsc-base/component/AnimateNum/index'
import symbolConfig from '../../../common/symbolConfig';
import commonService from '../../../common/commonService';

import filter from '../../../../grsc-base/util/filter';

import tpl from './tpl.html';
import lib from '../../../common/lib';

export default Vue.extend({
    name: 'IndexInfo',
    template: tpl,
    components: {},
    data() {
        return {
            symbolConfig: symbolConfig,
            hasInitCoins: false,
            coins: {},
            lib: lib,
            filter: filter,
            hasTriggerSlider: false     // 是否启用了价格走马灯
        };
    },
    created() {

        $(document).on('$$price', (event, coinsMap) => {
            this.hasInitCoins = true;
            let coins = _.cloneDeep(coinsMap);
            $.map(coins, (item, key) => {
                $.extend(item, {
                    firstOrder: {
                        price: '--',
                        priceText: '--',
                        type: 'BUY'
                    },
                    changeRatio: '--'   // 涨幅
                });
            });
            this.coins = coins;
            this._loopPrice();
        });

    },
    mounted() {

    },
    watch: {},
    methods: {

        /**
         * 跳转到市场页面
         * @param item
         */
        goPage(item) {
            window.location.href = './market.html?s=' + (item.assetCode2 + '_' + item.assetCode1);
        },

        _loopPrice(){
            let that = this;
            commonService.onLoopGetTradeDetail(rep => {
                that.isLoading = false;
                if (rep) {
                    let arr = rep;
                    $.map(this.coins, (item, key) => {
                        if (!arr.hasOwnProperty(item.symbol)) {
                            return;
                        }

                        let pr_ne = arr[item.symbol]['newPrice']; // 最新價
                        let pr_op = arr[item.symbol]['24Price']; // 開盤價

                        //最新价
                        item.firstOrder.price = filter.floorFixPrice(pr_ne, item[symbolConfig.PRICEPRECISION]);

                        // 修改涨幅向下取整的问题
                        let distance = pr_ne - pr_op;
                        let symbol = 1;
                        if (distance < 0) {
                            symbol = -1
                        }

                        if (arr[item.symbol]['24Price'] == 0) {
                            item.changeRatio = filter.floorFix(0,2);
                        } else {
                            item.changeRatio = filter.floorFix(symbol * (filter.abs(distance) / pr_op * 100), 2);
                        }
                    });

                }

                // 开启走马灯 add by jinguangguo@2018/01/18
                if (that.hasTriggerSlider === false) {
                    window.setTimeout(() => {
                        $(document).trigger('$$slide');
                    }, 1500);
                    that.hasTriggerSlider = true;
                }
            });

        }

    }

});